<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link type="text/css" href="jQuery-Timepicker-Addon/1.6.3/css/jquery-ui-timepicker-addon.css" rel="stylesheet"/>
    <link rel="stylesheet" href="select2/css/select2.css"/>
    <link rel="stylesheet" href="jqGrid/css/ui.jqgrid.css"/>
    <!--<link rel="stylesheet" href="jqGrid/css/ui.jqgrid-for-bootstrap4.css"/>-->
    <link rel="stylesheet" type="text/css" href="themes/<%= theme %>/jquery-ui.min.css"/>

    <script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="js/jquery-ui.js"></script>
    <script type="text/javascript" src="jqGrid/js/grid.locale-cn.js"></script>
    <script type="text/javascript" src="jqGrid/js/jquery.jqGrid.js"></script>
    <script type="text/javascript" src="select2/js/pinyin.min.js"></script>
    <script type="text/javascript" src="select2/js/select2.full.js"></script>
    <script type="text/javascript" src="select2/js/zh-CN.js"></script>
    <script type="text/javascript" src="jQuery-Timepicker-Addon/1.6.3/js/jquery-ui-timepicker-addon.js"></script>
    <script type="text/javascript" src="jQuery-Timepicker-Addon/1.6.3/js/jquery-ui-timepicker-zh-CN.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript" src="js/jsNodeId.js"></script>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
            font-size: small;
        }
        html,body{
            //overflow-x:hidden;
            -ms-overflow-style: scrollbar;
            height: 100%;
            box-sizing: border-box;
        }
    </style>
</head>
<body class="ui-widget-content">
<div id="list_1" style="padding:5px;">
    <table id="list"></table>
    <div id="pager"></div>
</div>
<script type="text/javascript">
	$(window).resize(function () {
		$(window).unbind("onresize");
		$("#list").jqGrid('setGridWidth', $("#list_1").width());//.jqGrid('setGridHeight', winWh.WinH-86);
		$(window).bind("onresize", this);
	});
	function getCustomersOptions() {
		let data = {};
		runAjax('get', '/salesMan/customers/lists', {}, function (err, result) {
			if (!err) {
				for (let i = 0; i < result.rows.length; i++) {
					data[result.rows[i]._id ] = result.rows[i].name;
				}
			}
		});
		return data;
	}
	function getGoodsOptions() {
		let data = {};
		runAjax('get', '/salesMan/goods/lists', {}, function (err, result) {
			if (!err) {
				for (let i = 0; i < result.rows.length; i++) {
					data[result.rows[i]._id ] = result.rows[i].name;
				}
			}
		});
		return data;
	}
	function getShopNames() {
		let data = {};
		runAjax('get','/salesMan/shops/lists',{},function (err,result) {
			if(!err){
				for (let i = 0; i < result.rows.length; i++) {
					data[result.rows[i]._id ] = result.rows[i].name;
				}
			}
		});
		return data;
	}
	function getTransportOptions() {
		let data = {};
		runAjax('get', '/salesMan/transport/lists', {}, function (err, result) {
			if (!err) {
				for (let i = 0; i < result.rows.length; i++) {
					data[result.rows[i]._id] = result.rows[i].name;
				}
			}
		});
		return data;
	}
	$(document).ready(function () {
		jQuery("#list").jqGrid({
			url:'/salesMan/lists', //
			datatype: "json",
			mtype : "GET",
			shrinkToFit:true,
			height: 'auto',
			width:"100%",
            deepempty:true,
			autowidth:true,
			/*postData:{Audit:1},*/
			colModel:[
				{label:'ID', name:'_id',index:'_id', hidden:true,editable : false}, //width:60
                {label:'subId', name:'subId',index:'_id', hidden:true,editable : false}, //width:60
				{label:'审核',name:'audit',index:'audit',editor:"text",width:60, editable : false,search:false,formatter:"select",edittype:"select",
                    editoptions: {
                        /*defaultValue: 1,*/
                        value: {1:'待审核',2:'已审核'}
                    }},
				{label:'日期',width:180,name:'date',index:'date',editor:"datetime", editable : false,search:true,
					formatter:function (cellvalue, options, rowObject) {
						return (new Date(cellvalue)).format("yyyy-MM-dd hh:mm:ss");
					},
					editoptions: {
						dataInit: function (elem) {
							$(elem).datetimepicker({
								defaultDate:+1,
								dateFormat: "yy-mm-dd",
								showSecond: true,
								changeYear:true,
								changeMonth:true,
								timeFormat: 'HH:mm:ss',
								stepHour: 1,
								stepMinute: 1,
								stepSecond: 1
							});
							if($(elem).val()==''){
								$(elem).datetimepicker('setDate',new Date());
							}
						}
					},
					searchoptions: {
						defaultValue:new Date(),
						dataInit: function (elem) {
							$(elem).datetimepicker({
								// defaultDate:new Date(),
								dateFormat: "yy-mm-dd",
								showSecond: true,
								changeYear:true,
								changeMonth:true,
								timeFormat: 'HH:mm:ss',
								stepHour: 1,
								stepMinute: 1,
								stepSecond: 1
							});
							$(elem).datetimepicker('setDate',new Date());
						},
						sopt: ["le", "ge"]
					},
					editrules:{required:true},
					editoptions: { defaultValue: (new Date()).format("yyyy-MM-dd hh:mm:ss")},
					formoptions:{elmprefix:'<span style="color: red;">*</span>'}
				},
                {label:'店铺',name:'sId',index:'sId',editor:"text", formatter:"select",edittype:"select",editable : false,
                    editoptions: {
                        value: getShopNames(),
                        dataInit: function (elem) {
                            $(elem).select2({
                                language: "zh-CN",
                                dropdownParent: $(elem).parent(),
                                placeholder: {
                                    id: '-1', // the value of the option
                                    text: '请选择一个选项'
                                },
                                allowClear: true,
                                // theme: "classic",
                            });
                        }
                    },editrules: {required: true},formoptions:{rowpos: 3, colpos: 1,
                        elmprefix:'<span style="color: red;">*</span>'},
                    stype:'select',
                    searchoptions: {
                        value: {},
                        dataInit: function (elem) {
                            $(elem).css({ width: "100%" }).select2({
                                language: "zh-CN",
                                dropdownParent: $(elem).parent(),
                                placeholder: {
                                    id: '-1', // the value of the option
                                    text: '请选择一个选项'
                                },
                                allowClear: true,
                                // theme: "classic",
                            });
                        },
                        sopt: ["eq", "ne"]
                    }
                },
				{label:'品类',name:'gId',index:'gId',editor:"text", formatter:"select",edittype:"select",editable : false,
					editoptions: {
						value: getGoodsOptions(),
						dataInit: function (elem) {
							$(elem).select2({
								language: "zh-CN",
								dropdownParent: $(elem).parent(),
								placeholder: {
									id: '-1', // the value of the option
									text: '请选择一个选项'
								},
								allowClear: true,
								// theme: "classic",
							});
						}
					},editrules: {required: true},formoptions:{rowpos: 3, colpos: 1,
						elmprefix:'<span style="color: red;">*</span>'},
					stype:'select',
					searchoptions: {
						value: {},
						dataInit: function (elem) {
							$(elem).css({ width: "100%" }).select2({
								language: "zh-CN",
								dropdownParent: $(elem).parent(),
								placeholder: {
									id: '-1', // the value of the option
									text: '请选择一个选项'
								},
								allowClear: true,
								// theme: "classic",
							});
						},
						sopt: ["eq", "ne"]
					}
				},
				{label:'客户',width:80,name:'cId',index:'cId',editor:"text", edittype:"select",editable : false,
                    formatter:function (cellvalue, options, rowObject) {
						var values = $("#list").jqGrid('getColProp','cId').editoptions.value;
						var value = values[cellvalue];
						if(!value){
							values[cellvalue] = cellvalue;
							value = cellvalue;
							$("#list").jqGrid('setColProp','cId',{searchoptions:{value:values}}); //setColProp('cId',{searchoptions:{value:values}});//
						}
						return value;
					},
					editoptions: {
						value:(function () {
                            return getCustomersOptions();
                        })() , // getCustomersOptions()
						dataInit: function (elem) {
							$(elem).select2({
                                /*width:"100%",*/
								language: "zh-CN",
                                dropdownParent: $(elem).parent(),
								dropdownParent: $(elem).parent(),
								placeholder: {
									id: '-1', // the value of the option
									text: '请选择一个选项'
								},
								allowClear: true,
								tags:true // 创建不存在的选项,相当于填写功能
							});
						}
					},
					searchoptions: {
						value:{}, // 创建完成后获取,见下面 loadComplete
						dataInit: function (elem) {
							$(elem).select2({
								language: "zh-CN",
                                dropdownParent: $(elem).parent(),
								/*width:"100%",*/
								dropdownParent: $(elem).parent(),
								placeholder: {
									id: '-1', // the value of the option
									text: '请选择一个选项'
								},
								allowClear: true,
								tags:true
							});
							$(elem).select2('val','-1');
						},
						sopt: ["eq", "ne"]
					},
					stype:'select',
                    editrules:{required:true},
                    formoptions:{elmprefix:'<span style="color: red;">*</span>'}
				},
				{label:'运输',name:'tId',index:'tId',editor:"text", edittype:"select",editable : true, formatter:"select",
					editoptions: {
						value:getTransportOptions(),
						dataInit: function (elem) {
							$(elem).select2({
								/*width:"100%",*/
								language: "zh-CN",
								dropdownParent: $(elem).parent(),
								dropdownParent: $(elem).parent(),
								placeholder: {
									id: '-1', // the value of the option
									text: '请选择一个选项'
								},
								allowClear: true,
								tags:true // 创建不存在的选项,相当于填写功能
							});
						}
					},
					searchoptions: {
						value:{}, // 创建完成后获取,见下面 loadComplete
						dataInit: function (elem) {
							$(elem).select2({
								language: "zh-CN",
								dropdownParent: $(elem).parent(),
								/*width:"100%",*/
								dropdownParent: $(elem).parent(),
								placeholder: {
									id: '-1', // the value of the option
									text: '请选择一个选项'
								},
								allowClear: true,
								tags:true
							});
							// $(elem).select2('val','-1');
						},
						sopt: ["eq", "ne"]
					},
					stype:'select',
					editrules:{required:true},
					formoptions:{elmprefix:'<span style="color: red;">*</span>'}
				},
				{
					label: '批零',
					name: 'mode',
					index: 'mode',
					width:60,
					editor: "text",
					editable: true,
					search: true,
					formatter: "select",
					edittype: "select",
					editoptions: {
						defaultValue:"0",
						value: {0: '批发', 1: '零售'}
					},
					stype:'select',
					searchoptions: {
						value: {0:'批发',1:'零售'},
						defaultValue:"0",
						sopt: ["eq"]
					},
					formoptions: {elmprefix: '<span style="color: red;">*</span>'}
				},
				{label:'支付',width:60,name:'payId',index:'payId',
                    editor:"text",search:false, formatter:"select",edittype:"select",editable : true,
					editoptions: {
						value: {0:"现金",1:"挂账",2:"支付宝",3:"微信",4:"其他"}
					},formoptions:{elmprefix:'<span style="color: red;">*</span>'}
				},
				{
					label: '数量',
					name: 'num',
					width:40,
					index: 'num',
					editor: "text",
					editable: false,
					formatter: "integer",
					search: false
				},
				{label:'应收',width:60,name:'should',index:'should',editor:"text", editable : false,search:false,
                    formatter:function(cellvalue, options, rowObject){
						return typeof cellvalue == 'object'?cellvalue.$numberDecimal:(cellvalue?cellvalue:"0.00");//代替默认
					}},
				{label:'实收',width:60,name:'proceed',index:'proceed',editor:"text", editable : false,search:false, editrules:{required:true},
					editoptions: { defaultValue: '0.00'},
                    formoptions:{elmprefix:'<span style="color: red;">*</span>'},
                    formatter:function(cellvalue, options, rowObject){
				        return typeof cellvalue == 'object'?cellvalue.$numberDecimal:(cellvalue?cellvalue:"0.00");
                    }},
				{label:'欠款',width:60,name:'arrears',index:'arrears',editor:"text",search:false, editable :false,
                    formatter:function(cellvalue, options, rowObject){
                        return typeof cellvalue == 'object'?cellvalue.$numberDecimal:(cellvalue?cellvalue:"0.00");
                    },
                    editrules:{required:true},editoptions: { defaultValue: '0.00'},
                    formoptions:{elmprefix:'<span style="color: red;">*</span>'}},
				{
					label: '备注', name: 'rem', index: 'rem', sortable: false, editable: true, search: false,
					edittype: "textarea",  editoptions: {rows: "2", cols: "23"}
				}
			],
			sortname:'_id',
			sortorder:'desc', // asc 升序
			viewrecords:true,
			rowNum:10,
			rowList:[10,20,30],
			pager:"#pager",
			caption: "销售单",
			multiselect: false,
            // toppager:true,
            editable: true,
			jsonReader: {
				id: "_id",
				root : "rows",
				page:"page",
				total:"total",
				records:"records",
				repeatitems: false
			},
			loadComplete:function(data){
			},
			onSelectRow:function (rowid,status) {
				// $("#list").jqGrid("expandSubGridRow", rowid);
			}
		});
        var values = $("#list").getColProp('cId').editoptions.value;
        $("#list").jqGrid('setColProp','cId',{searchoptions:{value:values}});
        values = $("#list").getColProp('sId').editoptions.value;
        $("#list").jqGrid('setColProp','sId',{searchoptions:{value:values}});
        values = $("#list").getColProp('gId').editoptions.value;
		$("#list").jqGrid('setColProp','gId',{searchoptions:{value:values}});
		values = $("#list").getColProp('tId').editoptions.value;
		$("#list").jqGrid('setColProp','tId',{searchoptions:{value:values}});

		jQuery("#list").jqGrid('navGrid','#pager',{add:false,edit:true,del:true,search:true,refresh:false},
			{checkOnSubmit:false, closeAfterEdit: true,reloadAfterSubmit:false, modal: true,
				url:"/salesMan",
				mtype:"put",
                editCaption:"销售管理",
                bSubmit:'确认',
                beforeShowForm : function(form) {
                    // $("#list").jqGrid('setColProp', 'proceed', { editrules:{required:true,number:true}});
					// form.find('#account').attr('readOnly',true);//把编辑框中的字段设置为只读的。
				},
				onclickSubmit: function(params, postData) {
					params.url = "/salesMan/" + postData.list_id;
					return [true,''];
				},
				beforeSubmit:function(postData, formid) {
			        var audit =$("#list").jqGrid("getCell", postData["list_id"], "audit");
					return [(audit == "1"),'已经审核不能编辑'];
				},
				afterSubmit:function(response, postData){
                    return [true,''];
				}
			}, // edit
			{checkOnSubmit:false, closeAfterAdd: true,reloadAfterSubmit:false,modal: true,
				jqModal: true,addedrow:'first',height:"100%",width:"100%",
				url:"salesMan", // /sales
				mtype:"post",
				beforeShowForm : function(form) {
				},
				onclickSubmit: function(params, postData) {
					return [true,''];
				},
				beforeSubmit:function(postData, formid) {
					return [true,'']; //return [true, "", jqXhr.responseJSON.id];
				},
				afterSubmit:function(response, postData){
					var result=response.responseJSON;
					// postData.id = result.id;
					return [true,"",postData._id];
				},
				afterComplete : function (response, postData, formid) {
                }
			}, // add
			{checkOnSubmit:false, reloadAfterSubmit:false,modal: true,
				url:"salesMan", // /sales
				mtype:"delete",
				onclickSubmit: function(params, id) {
                    var subId =$("#list").jqGrid("getCell", id, "subId");
                    params.url = "/salesMan/" + id + "/" + subId;
					return [true,''];
				},
                beforeSubmit: function (id, formid) {
                    var audit =$("#list").jqGrid("getCell", id, "audit");
                    return [(audit == "1"),'已经审核不能删除'];
                },
				afterSubmit:function(response, postData) {

					return [true, ''];
				}
			}, // del
			{multipleSearch: true, multipleGroup: false,closeAfterSearch:true,recreateForm:true
			} // search
		);
        });
</script>
</body>
</html>
